﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.IO;
using System.Windows;

namespace AMPLibrary
{
    public class Paths
    {
        // a string that holds the applications installed path
        public static string ApplicationPath
        {
            get;
            private set;
        }

        // a sting that will hold the name of the playlist save file
        public static string NowPlayingSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the name of the settings file
        public static string SettingsSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the serialized music collection
        public static string MusicCollectionSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the serialized video collection
        public static string VideoCollectionSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the serialized playlist collection
        public static string PlaylistCollectionSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the serialized playlist collection
        public static string AutomaticPlaylistCollectionSaveFile
        {
            get;
            private set;
        }

        // a string that will hold the serialized exclusion collection
        public static string ExcludedMediaCollectionSaveFile
        {
            get;
            private set;
        }

        // an object that gets the current operating system used by the application
        public static OperatingSystem OperatingSystem
        {
            get;
            private set;
        }

        // an object that contains information about who is using the application
        public static WindowsIdentity WindowsIdentity
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        public static void SetPaths()
        {
            OperatingSystem OperatingSystem = System.Environment.OSVersion;
            WindowsIdentity WindowsIdentity = WindowsIdentity.GetCurrent();

            ApplicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("AMP.exe", string.Empty);

            switch (OperatingSystem.Version.Major)
            {
                case 5: // windows xp
                    SettingsSaveFile = ApplicationPath + ApplicationFile.Settings;
                    NowPlayingSaveFile = ApplicationPath + ApplicationFile.NowPlayingData;
                    MusicCollectionSaveFile = ApplicationPath + ApplicationFile.MusicData;
                    VideoCollectionSaveFile = ApplicationPath + ApplicationFile.VideoData;
                    PlaylistCollectionSaveFile = ApplicationPath + ApplicationFile.PlaylistData;
                    AutomaticPlaylistCollectionSaveFile = ApplicationPath + ApplicationFile.AutomaticPlaylistData;
                    ExcludedMediaCollectionSaveFile = ApplicationPath + ApplicationFile.ExclusionMediaData;
                    break;

                case 6://vista and 7
                case 7:
                    string username = WindowsIdentity.Name.Split('\\')[1];
                    string systemDrive = Environment.GetEnvironmentVariable("windir").ToCharArray()[0].ToString();
                    string applicationData = systemDrive + ":\\users\\" + username + "\\appData\\local\\Adamo Technology\\AMP\\";

                    if (Directory.Exists(systemDrive + ":\\users\\" + username + "\\appData\\"))
                    {
                        if (!Directory.Exists(applicationData))
                            Directory.CreateDirectory(applicationData);

                        SettingsSaveFile = applicationData + ApplicationFile.Settings;
                        NowPlayingSaveFile = applicationData + ApplicationFile.NowPlayingData;
                        MusicCollectionSaveFile = applicationData + ApplicationFile.MusicData;
                        VideoCollectionSaveFile = applicationData + ApplicationFile.VideoData;
                        PlaylistCollectionSaveFile = applicationData + ApplicationFile.PlaylistData;
                        AutomaticPlaylistCollectionSaveFile = applicationData + ApplicationFile.AutomaticPlaylistData;
                        ExcludedMediaCollectionSaveFile = applicationData + ApplicationFile.ExclusionMediaData;
                    }
                    else
                    {
                        MessageBox.Show("Could not determine or could not access the temporary data path of your system.", "AMP Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        Application.Current.Shutdown();
                    }
                    break;

                default:
                    MessageBox.Show("Could not determine which operating system is in use.", "AMP Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    Application.Current.Shutdown();
                    break;
            }
        }
    }
}
